﻿using CodeFirst;
using DBManager;
using IRepository;
using IService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.Hosting;
using Model;
using ModelDto;
using Newtonsoft.Json.Linq;
using NPOI.SS.Formula;
using Repository;
using System.Collections.Generic;
using WebApplication1.Filter;
using WorkFlow;
using WorkFlow.WorkFlow;
using WorkflowCore.Interface;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    [ApiContentFilter]
    public class CeshiController : ControllerBase
    {
        private readonly ICeshiService _purchaseService;
        private readonly IDbContextFactory<MyDbContext> _DbContextFactory;
        private readonly IWorkflowHost _workflowHost;
        private static readonly object lock1 = new object();
        private static readonly object lock2 = new object();
        public CeshiController(ICeshiService purchaseService,
            IWorkflowHost workflowHost, IDbContextFactory<MyDbContext> _DbContextFactory)
        {
            this._DbContextFactory = _DbContextFactory;
            _purchaseService = purchaseService;
            _workflowHost = workflowHost;
        }

        [HttpGet]
        public string Get()
        {
            //var jsonTask = GetJsonAsync("http://localhost:5118/api/Ceshi/Get");
            return jsonTask.Result.ToString();
        }

        public static async Task<JObject> GetJsonAsync(string uri)
        {
            using (var client = new HttpClient())
            {
                var jsonString = await client.GetStringAsync(uri);
                return JObject.Parse(jsonString);
            }
        }
        //static void Method1()
        //{
        //    lock (lock1)
        //    {
        //        Thread.Sleep(100); 

        //        lock (lock2)
        //        {
        //            Console.WriteLine("Task 1 acquired lock2");
        //        }
        //    }
        //}

        //static void Method2()
        //{
        //    lock (lock2)
        //    {
        //        Thread.Sleep(100); 

        //        lock (lock1)
        //        {
        //            Console.WriteLine("Task 2 acquired lock1");
        //        }
        //    }
        //}

        /// <summary>
        /// 获取采购订单号
        /// </summary>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PurchaseOrderDto> GetPurchaseOrder(PurchaseRequestDto requestDto)
        {
            var db = _DbContextFactory.CreateDbContext();
            var data = await db.Set<PurchaseOrder>().AsQueryable().ToListAsync();
            //学习到的新写法,linq里面根据F某个字段获取新的数据，再取值return
            data.Select(f =>
            {
                var a = GetCeshi(f.Id);
                return new
                {
                    a.Capacity
                };
            }).ToList();


            return await _purchaseService.GetPurchase(requestDto.purchaseId);
        }

        private List<PurchaseOrder> GetCeshi(string id)

        {
            List<PurchaseOrder> a = new List<PurchaseOrder>();
            return a;
        }









        [HttpPost]
        public async Task<List<PurchaseOrderDto>> Excel()
        {
            return await _purchaseService.Excel();
        }
        [HttpPost]
        public ActionResult Import()
        {
            ExcelHelper a = new ExcelHelper();
            List<PurchaseOrder> purchaseOrders = new List<PurchaseOrder>()
            {
             new   PurchaseOrder()
             {
                 Order = "1",
                 SupplierCode = "1",
                 MaterialCode="1"
             }
            };
            var ms = a.import(purchaseOrders);

            return File(ms, "application/octet-stream", "filename.xlsx");
        }

    }
}
